package battleship.model;
/**
 * Ship Class.
 * All Carriers, Battleships, Destroyers, Patrol Boats, and Submarines inherit from this class.
 * @author Cameron Mcleod
 * @version 1.0
 */
public class Ship {
	//length
	int m_length;
	//name
	String m_Name;
	int Direction;
	/**
	 * 
	 * @param length the length of the ship
	 * @param Name the Name of the ship
	 */
	public Ship(int length, String Name){
		m_length = length;
		m_Name = Name;
	}
	/**
	 * 
	 * @return the length of the ship
	 */
	public int GetLength(){
		return m_length;
	}
	/**
	 * 
	 * @return the name of the ship
	 */
	public String GetName(){
		return m_Name;
	}
	/**
	 * 
	 * @param X the X location on the corresponding grid
	 * @param Y the Y location on the corresponding grid
	 * @param Dir The direction its facing
	 * @param grid The grid it is located on
	 * @return whether the ship was placed successfully or not
	 */
	public boolean Place(int X, int Y, int Dir, Grid grid){
		if(!IsValidPosition(X, Y, Dir, grid))
			return false;
		if(Dir == 0){ // Up
			for(int i = 0; i < m_length; i++){
				grid.TheGrid[X][Y-i].Occupy(m_Name);
			}
		}
		else if(Dir == 1){ // Down
			for(int i = 0; i < m_length; i++){
				grid.TheGrid[X][Y+i].Occupy(m_Name);
			}
		}
		else if(Dir == 2){ // Left
			for(int i = 0; i < m_length; i++){
				grid.TheGrid[X-i][Y].Occupy(m_Name);
			}
		}
		else if(Dir == 3){ //Right
			for(int i = 0; i < m_length; i++){
				grid.TheGrid[X+i][Y].Occupy(m_Name);
			}
		}
		return true;

	}
	/**
	 * 
	 * @param x the X location on the corresponding grid
	 * @param y the Y location on the corresponding grid
	 * @param dir The direction its facing
	 * @param grid The grid it is located on
	 * @return whether the ship can be placed in that postion
	 */
	public boolean IsValidPosition(int x, int y, int dir, Grid grid){
		if(x+ m_length >= 10 && dir == 3)
			return false;
		if(x - m_length < 0 && dir == 2)
			return false;
		if(y + m_length >= 10 && dir == 1)
			return false;
		if(y - m_length < 0 && dir == 0)
			return false;
		
		if(dir == 0){ // Up
			for(int i = 0; i <= m_length; i++){
				if(grid.TheGrid[x][y-i].IsOccupied())
					return false;
			}
		}
		else if(dir == 1){ // Down
			for(int i = 0; i <= m_length; i++){
				if(grid.TheGrid[x][y+i].IsOccupied())
					return false;
			}
		}
		else if(dir == 2){ // Left
			for(int i = 0; i <= m_length; i++){
				if(grid.TheGrid[x-i][y].IsOccupied())
					return false;
			}
		}
		else if(dir == 3){ //Right
			for(int i = 0; i <= m_length; i++){
				if(grid.TheGrid[x+i][y].IsOccupied())
					return false;
			}
		}
		return true;
	}
	/**
	 * Custom definion for any effects we may use.
	 * 
	 * @param grid The grid it is located on
	 * @param x the X location on the corresponding grid
	 * @param y the Y location on the corresponding grid
	
	 */
	public void OnShotAt(Grid grid, int x, int y){
		
	}

}
